package org.springframework.beans.factory.util;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanReference;

import java.util.*;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/12/13
 */
public class ValueUtils {
    public static Object getBaseValue(Class<?>type,Object realVal){
        if(type==int.class||type==Integer.class){
            String val = realVal.toString();
            if(StringUtils.isBlank(val)){
                val= "0";
            }
            realVal = Integer.parseInt(val);
        }else if(type==double.class||type==Double.class){
            String val = realVal.toString();
            if(StringUtils.isBlank(val)){
                val= "0.0";
            }
            realVal = Double.parseDouble(val);
        }else if(type==float.class||type==Float.class){
            String val = realVal.toString();
            if(StringUtils.isBlank(val)){
                val= "0.0";
            }
            realVal = Float.parseFloat(val);
        }else if(type==boolean.class||type==Boolean.class){
            String val = realVal.toString();
            if(StringUtils.isBlank(val)){
                val= "false";
            }
            realVal = Boolean.parseBoolean(val);
        }else if(type==long.class||type==Long.class){
            String val = realVal.toString();
            if(StringUtils.isBlank(val)){
                val= "0";
            }
            realVal = Long.parseLong(val);
        }else if(type==byte.class||type==Byte.class){
            String val = realVal.toString();
            if(StringUtils.isBlank(val)){
                val= "0";
            }
            realVal = Byte.parseByte(val);
        }else if(type==short.class||type==Short.class){
            String val = realVal.toString();
            if(StringUtils.isBlank(val)){
                val= "0";
            }
            realVal = Short.parseShort(val);
        }else if(type==char.class||type==Character.class){
            String val = realVal.toString();
            if(StringUtils.isBlank(val)){
                return 'a';
            }
            if(val.length()>1){
                throw new IllegalArgumentException("Can not set char field to String");
            }
            return val.charAt(0);
        }
        return realVal;
    }

    //集合处理太复杂，省略
    public static Object getRealValue(BeanFactory beanFactory, Object value) throws Exception {
        Object realValue = null;
        if(value==null){
            realValue = null;
        }
        else if (value instanceof BeanReference) {
            realValue = beanFactory.getBean(((BeanReference) value).getBeanName());
        }
        else if (value instanceof Object[]) {
            Object[] objs = (Object[]) value;
            for (int i=0;i<objs.length;i++) {
                objs[i] = getRealValue(beanFactory, objs[i]);
            }
            realValue = objs;
        } else if (value instanceof Collection) {
            Collection collection = (Collection) value;
            Object[] objs = collection.toArray();
            for (int i=0;i<objs.length;i++) {
                objs[i] = getRealValue(beanFactory, objs[i]);
            }
            //todo
            if(value instanceof List){
                List list= Arrays.asList(objs);
                if(value instanceof ArrayList){
                    realValue = Arrays.asList(objs);
                }else if(value instanceof LinkedList){
                    LinkedList ll = new LinkedList();
                    ll.addAll(list);
                    realValue = ll;
                }else if(value instanceof Vector){
                    Vector v = new Vector();
                    v.addAll(list);
                    realValue = v;
                }
            }else if(value instanceof Set){
                if(value instanceof HashSet){

                }else if(value instanceof LinkedHashSet){

                }else if(value instanceof SortedSet){

                }else if(value instanceof TreeSet){

                }
            }

        } else if (value instanceof Properties) {
            // TODO 处理properties中的bean引用
        } else if (value instanceof Map) {
            // TODO 处理Map中的bean引用
        } else{
            realValue = value;
        }
        return realValue;
    }

}
